package views;

import bean.Cls;
import bean.Team;
import service.ClassService;
import utils.CardSecretGenerator;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddTeam extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    Cls cls2=new Cls();// 用于存储班级信息

    //设置班级信息
    public  void setCls(Cls cls) {
        cls2=cls;

    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddTeam frame = new AddTeam();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

//创建窗口
    public AddTeam() {
        setTitle("新增小组");// 设置窗口标题
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 375, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("新增小组");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        lblNewLabel.setBounds(134, 10, 174, 58);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("小组名称：");
        lblNewLabel_1.setBounds(42, 84, 73, 15);
        contentPane.add(lblNewLabel_1);

        textField = new JTextField();
        textField.setBounds(105, 81, 203, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("新增");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 检查小组名称是否为空
                if(textField.getText().length()==0) {
                    JOptionPane.showMessageDialog(null,"小组名称不能为空！！","提示",JOptionPane.PLAIN_MESSAGE);
                }else {
                    //创建一个对象封装数据
                    Team group=new Team();
                    group.setC_id(cls2.getC_id());
                    group.setC_name(cls2.getC_name());
                    CardSecretGenerator cardSecretGenerator=new CardSecretGenerator();
                    group.setT_id(cardSecretGenerator.Rnumber());
                    group.setT_name(textField.getText());
                    //打印Cls对象
                    System.out.println(group);
                    ClassService cs=new ClassService();
                    int n=cs.insertGroup(group);
                    if(n>0) {
                        JOptionPane.showMessageDialog(null,"新增小组成功！！","提示",JOptionPane.PLAIN_MESSAGE);
                    }else {
                        JOptionPane.showMessageDialog(null,"新增小组失败！！","提示",JOptionPane.PLAIN_MESSAGE);
                    }
                }

            }
        });
        btnNewButton.setBounds(80, 230, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("返回");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Main m=new Main();// 返回时传递班级信息
                m.setCls(cls2);
                m.setVisible(true);
                AddTeam.this.dispose();// 关闭当前窗口
            }
        });
        btnNewButton_1.setBounds(183, 230, 93, 23);
        contentPane.add(btnNewButton_1);
    }

}
